import { CacheEnum } from "@/enum/userEnum"
import utils from "@/utils"
import { http } from "../plugins/axios"


interface IUser{    
           nickName: string   
           avatar:string
}

interface IBok{
  b_id:number
  u_id:number
  title:string
  content:string
  bok_create_time:string
  comments:number
  goods:number
  views:number
  user:IUser
}
//返回的数据类型
export interface IArticle{
   records:IBok[]
   currentPage:number
}


//表单传到后台服务器,默认当前是10个数据为1页，访问网站当前第一页
function article(value:number){
      return http.request<IArticle[]>({
        url:`bok`,
        method: "get",
        params:{pageNum:value},
        headers: {token:utils.store.get(CacheEnum.TOKEN_NAME)}
      })
}

//获取当天的bok
function allArticle(){
   return http.request<IArticle[]>({
    url:`allBok`,
    method: "get",
    headers: {token:utils.store.get(CacheEnum.TOKEN_NAME)}
   })
}


//点赞的泛型

interface IGoods{
  code:number
  msg:string
}

//点赞
function changeGoods(b_id:number,u_id:number){
return http.request<IGoods>({
  url:'goods',
  method: 'get',
  params:{b_id:b_id,u_id:u_id},
  headers: {token:utils.store.get(CacheEnum.TOKEN_NAME)}
})
}


//文章展示页面的点赞数和用户头像泛型
interface IGoodsAvatar{
    avatarList:[]
}


//通过b_id获取当前博客的点赞数和u_id对应的头像
function getGoodsAndAvatar(b_id:any){
   return http.request<IGoodsAvatar>({
        url:'getGoodsInfo',
        method:'get',
        params:{b_id:b_id},
        headers:{token:utils.store.get(CacheEnum.TOKEN_NAME)}
   })
}


//通过当前的b_id获取当前博客的评论信息(比如:u_id,头像，nickname 和 当前用户评论的内容 评论时间)

function getCommentsInfoByBid(b_id:any){
  return http.request({
    url:'getCommentsInfoByBid',
    method:'get',
    params:{b_id:b_id},
    headers:{token:utils.store.get(CacheEnum.TOKEN_NAME)}
})
}

//发送请求,写评论，传入当前博客的b_id,传入当前的u_id,传入当前的评论内容

function setComment(b_id:any,u_id:any,comment:any){
  return http.request({
    url:"setComment",
    method:"get",
    params:{b_id:b_id,u_id:u_id,comment:comment},
    headers:{token:utils.store.get(CacheEnum.TOKEN_NAME)}
  })

}

//删除评论
function deleteComment(c_id:any,b_id:any){
  return http.request({
    url:"deleteComment",
    method:"get",
    params:{c_id:c_id,b_id:b_id},
    headers:{token:utils.store.get(CacheEnum.TOKEN_NAME)}
  })
}


//浏览记录
function storeViews(b_id:any,u_id:any){
    return http.request({
      url:"setView",
      method:"get",
      params:{b_id:b_id,u_id:u_id},
      headers:{token:utils.store.get(CacheEnum.TOKEN_NAME)}
    })
}

//保存文章
function storeTopic(u_id:any,title:any,content:any){
    return http.request({
      url:'setTopic',
      method:"get",
      params:{u_id:u_id,title:title,content:content},
      headers:{token:utils.store.get(CacheEnum.TOKEN_NAME)}
    })
}


function  getbokBybid(b_id:any){
   return http.request({
    url:'getbokBybid',
    method:"get",
    params:{b_id:b_id},
    headers: {token:utils.store.get(CacheEnum.TOKEN_NAME)}
   })
}

export default {article,allArticle,changeGoods
  ,getGoodsAndAvatar,getCommentsInfoByBid,setComment
  ,deleteComment,storeViews,storeTopic,getbokBybid}